﻿Imports System.Net.Http
Imports AntdUI

Public Class Form1

    ''' <summary>
    ''' 多轮对话记录,上一轮的结果
    ''' </summary>
    Private resultMsg As New Text.StringBuilder

    ''' <summary>
    ''' api密钥
    ''' </summary>
    Private apikey As String = "sk-WyUd666666666666CqwoGUoJ81k9C3x3wr"

    ''' <summary>
    ''' kimi对象
    ''' </summary>
    ''' <returns></returns>
    Private Property kimi As MoonshotKimi

    ''' <summary>
    ''' 窗体加载事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        kimi = New MoonshotKimi(apikey)
        ''------------------------
        Try
            Dim bl As Balance = Await kimi.GetBalance
            Label2.Text = "￥" & Math.Round(bl.Data.AvailableBalance, 2)
        Catch ex As Exception
            AntdUI.Message.error(Me, ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' 提问
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim q As String = Input1.Text
        ChatList1.AddToBottom(New AntdUI.Chat.TextChatItem(q, My.Resources.logo, "我") With {.[Me] = True})
        ''-----------
        Dim chatReq As New ChatRequest With {
                         .Messages = New List(Of ChatRequest.MessagesType) From {
                            New ChatRequest.MessagesType With {
                                .Role = ChatRequest.RoleEnum.System,
                                .Content = "你是一个Excel办公助手,仅回答办公相关的内容,其他无关内容忽略。"
                            },
                            New ChatRequest.MessagesType With {
                                .Role = ChatRequest.RoleEnum.User,
                                .Content = q
                            }
                         },
                         .Model = ChatRequest.ModelEnum.MoonShotV1_32k,
                         .Stream = True
                     }
        '' 多轮对话
        If Not String.IsNullOrWhiteSpace(resultMsg.ToString) Then
            chatReq.Messages.Add(New ChatRequest.MessagesType With {
                                .Role = ChatRequest.RoleEnum.Assistant,
                                .Content = resultMsg.ToString
                            })
        End If

        '' 清除对话
        Input1.Clear()
        ''------------
        Dim msgText As New AntdUI.Chat.TextChatItem("", My.Resources.favicon, "Kimi")
        ChatList1.AddToBottom(msgText)
        msgText.Loading = True
        ''------------
        Await kimi.ChatStream(chatReq, openedCallBack:=Sub(state As String) ''打开状态
                                                           'Console.WriteLine(state)
                                                       End Sub,
                                                  closedCallBack:=Async Sub(state) ''关闭状态
                                                                      'Console.WriteLine(state)
                                                                      msgText.Loading = False
                                                                      resultMsg.Clear() ''拼接完成则清空,进行下一轮拼接
                                                                      ''-------
                                                                      Try
                                                                          Dim bl As Balance = Await kimi.GetBalance
                                                                          Label2.Text = "￥" & Math.Round(bl.Data.AvailableBalance, 2)
                                                                      Catch ex As Exception
                                                                          AntdUI.Message.error(Me, ex.Message)
                                                                      End Try
                                                                  End Sub,
                                                  msgCallback:=Sub(res As ChatResponse) ''接收信息
                                                                   Dim msg As String = res.Choices.First?.Delta?.Content
                                                                   resultMsg.Append(msg)
                                                                   ''----------
                                                                   Dim isbut As Boolean = ChatList1.IsBottom
                                                                   msgText.Text &= msg
                                                                   If isbut Then ChatList1.ToBottom()
                                                                   'Console.WriteLine(msg)
                                                               End Sub,
                                                  errorCallback:=Sub(ex As String) ''异常处理
                                                                     'Console.WriteLine(ex.Message)
                                                                     AntdUI.Message.error(Me, ex)
                                                                 End Sub)

    End Sub

    ''' <summary>
    ''' 复制最后一个结果
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If ChatList1.Items.Count > 0 Then
            Dim chat As AntdUI.Chat.TextChatItem = ChatList1.Items.Last
            If chat.Me = False AndAlso Not String.IsNullOrWhiteSpace(chat.Text) Then
                Me.ClipboardSetText(chat.Text)
                AntdUI.Message.success(Me, "复制成功")
            End If
        Else
            AntdUI.Message.warn(Me, "没有对话结果")
        End If
    End Sub
End Class
